package com.colorfruit.admin.control.controller;

import com.colorfruit.admin.business.service.AddressService;
import com.colorfruit.admin.pojo.entity.FoundationPickGoodsAddress;
import com.colorfruit.admin.pojo.entity.FoundationUserReceiveAddress;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @Autowired
    HttpServletRequest request;

    @GetMapping("/selectUserAddressList")
    public BaseResult selectUserAddressList(){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] selectUserAddressList param: userId = {}", userId);
            List<FoundationUserReceiveAddress> foundationUserReceiveAddresses = addressService.selectUserAddressList(userId);
            log.info("# [OUT] [controller] selectUserAddressList SUCCESS");
            return new BaseResult(foundationUserReceiveAddresses);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @PostMapping("/addUserAddress")
    public BaseResult addUserAddress(@Valid @RequestBody FoundationUserReceiveAddress foundationUserReceiveAddress){
        try {
            String userId = request.getHeader("userId");
            foundationUserReceiveAddress.setUserId(userId);
            log.info("# [IN] [controller] addUserAddress param: foundationUserReceiveAddress = {}", foundationUserReceiveAddress.toString());
            BaseResult baseResult = addressService.addUserReceiveAddress(foundationUserReceiveAddress);
            log.info("# [OUT] [controller] addUserAddress SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }


    @PostMapping("/editUserAddress")
    public BaseResult editUserAddress(@Valid @RequestBody FoundationUserReceiveAddress foundationUserReceiveAddress){
            String userId = request.getHeader("userId");
            foundationUserReceiveAddress.setUserId(userId);
            log.info("# [IN] [controller] editUserAddress param: foundationUserReceiveAddress = {}", foundationUserReceiveAddress.toString());
            BaseResult baseResult = addressService.editUserReceiveAddress(foundationUserReceiveAddress);
            log.info("# [OUT] [controller] editUserAddress SUCCESS");
            return baseResult;
    }

    @GetMapping("/deleteUserReceiveAddress")
    public BaseResult deleteUserReceiveAddress(Long addressId ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] deleteUserReceiveAddress param: userId = {}", userId);
            BaseResult baseResult = addressService.deleteUserReceiveAddress(userId, addressId);
            log.info("# [OUT] [controller] deleteUserReceiveAddress SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/getDefaultAddress")
    public BaseResult getDefaultAddress( ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] getDefaultAddress param: userId = {}", userId);
            FoundationUserReceiveAddress defaultAddress = addressService.getDefaultAddress(userId);
            log.info("# [OUT] [controller] getDefaultAddress SUCCESS");
            return new BaseResult(defaultAddress);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/setDefaultAddress")
    public BaseResult setDefaultAddress( Long addressId ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] setDefaultAddress param: userId = {}", userId);
            BaseResult baseResult = addressService.setDefaultAddress(userId, addressId);
            log.info("# [OUT] [controller] setDefaultAddress SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }


    @GetMapping("/selectPickGoodsAddressList")
    public BaseResult selectPickGoodsAddressList( ){
        try {
            log.info("# [IN] [controller] selectPickGoodsAddressList ");
            List<FoundationPickGoodsAddress> foundationPickGoodsAddresses = addressService.selectPickGoodsAddressList();
            log.info("# [OUT] [controller] selectPickGoodsAddressList SUCCESS");
            return new BaseResult(foundationPickGoodsAddresses);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/getPickGoodsAddress")
    public BaseResult getPickGoodsAddress(Long id){
        try {
            log.info("# [IN] [controller] getPickGoodsAddress ");
            FoundationPickGoodsAddress pickGoodsAddress = addressService.getPickGoodsAddress(id);
            log.info("# [OUT] [controller] getPickGoodsAddress SUCCESS");
            return new BaseResult(pickGoodsAddress);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

}
